/** 行
*	@param {String} url 路径地址
*	@param {Function} funSuccess 成功的回调
*	@param {Function} funComplete 方法完成
*/
function get(url,funSuccess,funComplete){
	uni.showLoading({
		title: '加载....',
		mask: false
	});
	uni.request({
		url: url,
		method: 'GET',
		sslVerify:false,
		header:{
			"content-type":"application/json;charset:UTF-8"
		},
		data: {},
		success: res => {
			funSuccess(res.data);
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete();
		}
	});
}


function getWithHeader(url,header,funSuccess,funComplete){
	uni.showLoading({
		title: '加载....',
		mask: false
	});
	uni.request({
		//url:url  参数名和变量相同时可省略
		url,
		header,
		method: 'GET',
		sslVerify:false,
		success: res => {
			funSuccess(res.data);
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete();
		}
	});
	
}


/**
 * @param {Object} url	接口地址
 * @param {Object} data	要提交的数据
 * @param {Object} funSuccess	成功回调
 * @param {Object} funComplete	完成回调
 */


function postParam(url,data,funSuccess,funComplete){
	uni.showLoading({
		title: '发送请求...',
		mask: false
	});
	uni.request({
		url,
		method: 'POST',
		header:{
			/* q=qualify*/
			"Accept":"application/json;q=0.9, */*; q=0.1",
			"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
		},
		data,
		sslVerify:false,
		success: res => {
			funSuccess(res.data);
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete();
		}
	});
	
}

/**
 * @param {String} url	接口地址
 * @param {JSON} data	要提交的数据
 * @param {Function} funSuccess	成功回调
 * @param {Function} funComplete	完成回调
 */
function jsonParam(url,data,funSuccess,funComplete){
	uni.showLoading({
		title: '发送请求...',
		mask: false
	});
	uni.request({
		url,
		method: 'POST',
		data,
		sslVerify:false,
		success: res => {
			funSuccess(res.data);
		},
		fail: err => {
		},
		complete: e => {
			uni.hideLoading();
			funComplete();
		}
	});
}

/**
 * @param {Object} url
 * @param {Object} data
 * @param {Object} funSuccess
 * @param {Object} funComplete
 */
function deleteRequire(url,data,funSuccess,funComplete){
	uni.showLoading({
		title: '发送请求...',
		mask: false
	});
	uni.request({
		url,
		data,
		method: 'DELETE',
		header:{
			/* q=qualify*/
			"Accept":"application/json;q=0.9, */*; q=0.1",
			"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
		},
		sslVerify:false,
		success: res => {
			funSuccess(res.data);
		},
		fail: err => {
			console.log(err);
		},
		complete: () => {
			uni.hideLoading();
			funComplete();
		}
	});
}

function postFile(url,name,filePath,data,funSuccess){
	uni.uploadFile({
		url,
		name,
		filePath,
		formData:data,
		success:res => funSuccess(res)
	})
}

/**
 * 同步写法实现异步请求，async块内部代码是同步执行的，会等待一步await代码行的执行接口
 * 如果在lambda表达式内使用，async应该写在匿名函数参数的前面
 */
function requestAsync(url,data={},method="GET"){
	return new Promise((resolve,reject) => {
		uni.request({
			url,
			method,
			data,
			success: res => {
				resolve(res)
			},
			fail: err => {
				reject(err)
			},
			complete: () => {}
		});
	})
}

module.exports = {
	get,
	getWithHeader,
	postParam,
	jsonParam,
	deleteRequire,
	postFile,
	requestAsync,
}